//
//  CollectionViewController.m
//  SunXi
//
//  Created by dllo on 15/11/28.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "CollectionViewController.h"

@interface CollectionViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSMutableArray *musicArr;
@property(nonatomic, retain) NSMutableArray *readArr;
@property (nonatomic, strong)CoreDataManager *manager;
@property(nonatomic, copy) NSString *type;
@end

@implementation CollectionViewController{
    UIButton *_musicbutton;
    UIButton *_webbutton;
    UIButton *_moviebotton;
    NSArray *_collectmusicArr;
    NSArray *_webArr;
    NSArray *_deletemusciarr;
    NSArray *_movieArr;
    UIView *_nilview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    SV.BC = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT - 104) style:UITableViewStylePlain];
    self.title = @"收藏";
    self.manager = [CoreDataManager defaults];
    self.type = @"阅读";
    [SV addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    [self initbutton];
    [self initnil];
}

- (void)initbutton{
   
    _webbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    _webbutton.frame = CGRectMake(0, 0, WIDTH / 3, 40);
    [SV addSubview:_webbutton];
    _webbutton.backgroundColor = BUTTONCOLOR;
    [_webbutton setTintColor:[UIColor whiteColor]];
    [_webbutton setTitle:@"阅读" forState:0];
    _webArr = [self lookWeb];
    _collectmusicArr = [self lookmusic];
    _movieArr = [self lookmovie];
    [_webbutton setEnabled:NO];
    [_webbutton addTarget:self action:@selector(takeWebTableView:) forControlEvents:UIControlEventTouchUpInside];
    
    
    _musicbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    _musicbutton.frame = CGRectMake(WIDTH / 3, 0, WIDTH / 3, 40);
    [SV addSubview:_musicbutton];
    _musicbutton.backgroundColor = BUTTONCOLOR;
    [_musicbutton setTintColor:[UIColor whiteColor]];
    [_musicbutton setTitle:@"电台" forState:0];
    [_musicbutton addTarget:self action:@selector(takeMusixTableView:) forControlEvents:UIControlEventTouchUpInside];
    
    _moviebotton = [UIButton buttonWithType:UIButtonTypeSystem];
    _moviebotton.frame = CGRectMake(WIDTH / 3 * 2, 0, WIDTH / 3, 40);
    [SV addSubview:_moviebotton];
    _moviebotton.backgroundColor = BUTTONCOLOR;
    [_moviebotton setTintColor:[UIColor whiteColor]];
    [_moviebotton setTitle:@"视频" forState:0];
    [_moviebotton addTarget:self action:@selector(takeMovieTableView:) forControlEvents:UIControlEventTouchUpInside];
    
    
}
- (void)takeWebTableView:(UIButton *)button{
    [button setEnabled:NO];
    self.type = @"阅读";
    [_musicbutton setEnabled:YES];
    [_moviebotton setEnabled:YES];
    [self.tableView reloadData];
}
- (void)takeMusixTableView:(UIButton *)button{
    [button setEnabled:NO];
    self.type = @"音乐";
    [_webbutton setEnabled:YES];
    [_moviebotton setEnabled:YES];
    [self.tableView reloadData];
}
- (void)takeMovieTableView:(UIButton *)button{
    [button setEnabled:NO];
    [_webbutton setEnabled:YES];
    [_musicbutton setEnabled:YES];

    self.type = @"视频";
    [self.tableView reloadData];

}
//MARK:分几个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSArray *)lookWeb{
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Collect"];
    
    //添加约束  谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@",@"type",@"%@"],@"web"];
    request.predicate = predicate;
    
    NSError *error;
    NSArray *arr = [[self.manager managedObjectContext] executeFetchRequest:request error:&error];
    
    return arr;
}

- (NSArray *)lookmovie{
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Collect"];
    
//    添加约束  谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@",@"type",@"%@"],@"movie"];
    request.predicate = predicate;
    
    NSError *error;
    NSArray *arr = [[self.manager managedObjectContext] executeFetchRequest:request error:&error];
    
    return arr;
}
- (NSArray *)lookmusic{
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Collect"];
    
    //添加约束  谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@",@"type",@"%@"],@"music"];
    request.predicate = predicate;
    
    NSError *error;
    _deletemusciarr = [[self.manager managedObjectContext] executeFetchRequest:request error:&error];
    NSMutableArray *rearr = [NSMutableArray array];
    for (Collect *ect in _deletemusciarr ) {
        RadioModel *model = [[RadioModel alloc]init];
        model.radioid = ect.radio;
        model.musicUrl = ect.code;
        model.title = ect.title;
        NSDictionary *dicfir = @{@"uname":ect.user};
        NSDictionary *dictwo = @{@"userinfo":dicfir};
        model.userinfo = dictwo;
        model.coverimg = ect.image;
        [rearr addObject:model];
    }
    NSArray *qq = [NSArray arrayWithArray:rearr];
    return qq;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.type isEqualToString: @"阅读"]) {
        if (_webArr.count < 1) {
            _nilview.hidden = NO;
        }else{
            _nilview.hidden = YES;
        }
        return _webArr.count;
    }else if ([self.type isEqualToString:@"视频"]){
        if (_movieArr.count < 1) {
            _nilview.hidden = NO;
        }else{
            _nilview.hidden = YES;
        }
        return _movieArr.count;
    }
    if (_collectmusicArr.count < 1) {
        _nilview.hidden = NO;
    }else{
        _nilview.hidden = YES;
    }

    return _collectmusicArr.count;
}
- (void)initnil{
    if (!_nilview) {
    _nilview = [[UIView alloc]initWithFrame:self.tableView.frame];
    _nilview.backgroundColor = [UIColor whiteColor];
        [SV addSubview:_nilview];
        UIImageView *aimageView = [[UIImageView alloc]init];
        [_nilview addSubview:aimageView];
        [aimageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(100, 100));
            make.center.equalTo(_nilview);
        }];
        
        aimageView.image = [UIImage imageNamed:@"hezi"];
    }
    UILabel *label = [[UILabel alloc]init];
    [_nilview addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.and.left.equalTo(_nilview);
        make.height.mas_equalTo(20);
        make.centerY.equalTo(_nilview).offset(-120);
    }];
    label.textAlignment = 1;
    label.text = @"收藏空空";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        
    }
    Collect *model;
    if ([self.type isEqualToString:@"阅读"]) {
        model = _webArr[indexPath.row];
    }else if ([self.type isEqualToString:@"视频"]){
        model = _movieArr [indexPath.row];
    }else{
        model = _collectmusicArr[indexPath.row];
    }
    cell.textLabel.text = model.title;
    return cell;
}

/*
 RadioplayViewController *playVC = [[RadioplayViewController alloc]init];
 [RadioSavemodel shareSingleton].playlistArr = _arr;
 [RadioSavemodel shareSingleton].playNumber = indexPath.row;
 [self.navigationController pushViewController:playVC animated:YES];
 */

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.type isEqualToString:@"阅读"] ) {
        WebView *webVC = [[WebView alloc]init];
        Collect *ect = _webArr[indexPath.row];
        webVC.url = ect.code;
        webVC.tittle = ect.title;
        [self.navigationController pushViewController:webVC animated:YES];
    }else if ([self.type isEqualToString:@"视频"]){
        MVDetailsViewController *deVC = [[MVDetailsViewController alloc]init];
        Collect *ect = _movieArr[indexPath.row];
        deVC.str = ect.code;
        deVC.mytitle = ect.title;
        [self.navigationController pushViewController:deVC animated:YES];
    }else{
        RadioplayViewController *playVC = [[RadioplayViewController alloc]init];
        [RadioSavemodel shareSingleton].playlistArr = [NSMutableArray arrayWithArray:_collectmusicArr];
        [RadioSavemodel shareSingleton].playNumber = indexPath.row;
        [self.navigationController pushViewController:playVC animated:YES];
    }
}

- (IBAction)presentLeftMenuViewController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}
@end
